package com.finalcial.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.finalcial.po.GetSingMsgJson;
import com.finalcial.po.InsertSuppJSON;
import com.finalcial.po.LoginMsg;
import com.finalcial.service.ApplicationService;
import com.finalcial.service.DeclarationInformationService;
import com.finalcial.util.PageUtil;

@Controller
@RequestMapping("declarationInformation")
public class DeclarationInformationController {
	
	@Resource
	private DeclarationInformationService declarationInformationService;
	@Resource
	private ApplicationService applicationService;
	/**
	 * 申报界面信息
	 * 根据员工的id查询出所有的报销信息(直接查询报销表的id)session 
	 * @return 
	 */
	@RequestMapping("/listSingMsgByStaff")
	public String listSingMsgByStaff(HttpServletRequest request, HttpServletResponse response,HttpSession httpSession){
		
		LoginMsg loginMsg = (LoginMsg) httpSession.getAttribute("LoginMsg");
		Integer userId = loginMsg.getUserId();	
		String pageNow = request.getParameter("pageNow");//得到当前页
		PageUtil page = null;
		List<Map<String, Object>> declarationInfoList = null;
		
		Integer totalCount = declarationInformationService.listSingMsgByStaffCounts(userId);  //查询总条数
		if(pageNow != null) {
			page = new PageUtil(totalCount, Integer.parseInt(pageNow));
			declarationInfoList = declarationInformationService.listSingMsgByStaff(userId, page.getStartPos(), page.getPageSize());//得到一段查询信息   page.getStartPos(), page.getPageSize() 参数
		}else {
			page = new PageUtil(totalCount, 1);
			declarationInfoList = declarationInformationService.listSingMsgByStaff(userId, page.getStartPos(), page.getPageSize());
		}
		for(Map<String, Object> map:declarationInfoList) {
			for(String s :map.keySet()) {
				System.out.println(map.get(s));
			}
		}
		request.setAttribute("page", page);
		request.setAttribute("declarationInfoList", declarationInfoList);
		return "expenseplatform/declarationinformation/declarationinformation";
	
	
	}
	
	/**
	 * 流程跟踪--点击某一行获取相应的报账id、报账类型名--直接显示在流程跟踪的
	 * 显示流程追踪表信
	 * 角色名(通过审批表的userId)、部门名(通过审批表的userId)、用户名(通过审批表的userId)、审批意见、审批状态、审批时间、工作状态名、报销时间
	 */
	@RequestMapping("/listProcessMsg")
	public ModelAndView listProcessMsg(@RequestParam("reimbursementId") String reimbursementId){
		ModelAndView mav = new ModelAndView();
		List<Map<String, Object>> listProcessMsg =  declarationInformationService.listProcessMsg(reimbursementId);
		mav.addObject("listProcessMsg", listProcessMsg);
		mav.setViewName("expenseplatform/declarationinformation/processtracking");
		return mav;
	}
	
	/**
	 * 申报信息查看详情
	 * @return
	 */
	@RequestMapping("/listdetails")
	public ModelAndView listdetails(HttpServletResponse response,HttpServletRequest request){
		ModelAndView mav = new ModelAndView();
		//显示接收人的角色
		List<Map<String, Object>> roleList = applicationService.showRoleNameList();
		
		//显示基础信息
		String reimbursementId  = request.getParameter("reimbursementId");
		String reimbursementTypeName  =request.getParameter("reimbursementTypeName");
		/*try {
			reimbursementTypeName = new String(reimbursementTypeName.getBytes("iso-8859-1"),"utf-8");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}*/
		String reimbursementName =reimbursementTypeName; 
		List<Map<String, Object>> listdetails = declarationInformationService.getEssentialInformation(reimbursementId);
		//内部人信息
		List<Map<String, Object>> listOtherInformation = declarationInformationService.getOtherInformation(reimbursementId);
		//解析图片文件
		List<Map<String, Object>> imageList = applicationService.getCheckSheet(reimbursementId);
				
		mav.addObject("listdetails", listdetails);
		mav.addObject("listOtherInformation", listOtherInformation);
		mav.addObject("roleList", roleList);
		mav.addObject("imageList", imageList);
		if("国内差旅".equals(reimbursementName)){
			mav.setViewName("expenseplatform/declarationinformation/domestictravelSingle");
		}else if("公务交通".equals(reimbursementName)){
			mav.setViewName("expenseplatform/declarationinformation/officialtrafficSingle");
		}else if("物业管理".equals(reimbursementName)){
			mav.setViewName("expenseplatform/declarationinformation/propertyManagerApplySingle");
		}else if("员工福利".equals(reimbursementName)){
			mav.setViewName("expenseplatform/declarationinformation/staffFormApplySingle");
		}
		
		return mav;
	}
	
	/**
	 * 多条件查询
	 */
	@RequestMapping("getSingMsg")
	public @ResponseBody List<Map<String, Object>> getSingMsg(@RequestBody GetSingMsgJson getSingMsgJson,HttpSession httpSession){
	    LoginMsg loginMsg = (LoginMsg) httpSession.getAttribute("LoginMsg");
		Integer userId =  loginMsg.getUserId();
		String userName= getSingMsgJson.getUserName();
		Integer startMoney = getSingMsgJson.getStartMoney();
		Integer endMoney = getSingMsgJson.getEndMoney();
		String approvalStateName = getSingMsgJson.getApprovalStateName();
		String reimbursementTypeName = getSingMsgJson.getReimbursementTypeName();
		List<Map<String, Object>> listSingMSg = declarationInformationService.getSingMsg(userId, userName, startMoney, endMoney, approvalStateName,reimbursementTypeName);
		return listSingMSg;
	}
	
	/**
	 * 添加补单信息
	 * @param insertSuppJSON
	 * @return
	 */
	@RequestMapping("/insertsupplementMs")
	@ResponseBody
	public Integer insertsupplementMs(@RequestBody InsertSuppJSON insertSuppJSON){
		String reimbursementId = insertSuppJSON.getReimbursementId();//报账id
		String supplementReason = insertSuppJSON.getSupplementReason();//补单id
		String invoiceIdExplain = insertSuppJSON.getInvoiceIdExplain();//补单说明
		Integer invoiceNumber = insertSuppJSON.getInvoiceNumber();//补单张数
		String supplementTimes = insertSuppJSON.getSupplementTime();//补单时间
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");//小写的mm表示的是分钟
		Date supplementTime = null;
		try {
			supplementTime = sdf.parse(supplementTimes);
		} catch (ParseException e) {
			
			e.printStackTrace();
		}  
	    
		Integer invoiceMoney = insertSuppJSON.getInvoiceMoney();//补单金额
		String workStateName = "待阅";
		Integer userId = insertSuppJSON.getUserId();//用户id
		System.out.println(userId);
		Integer total = declarationInformationService.insertsupplementMs(reimbursementId, supplementReason, invoiceIdExplain, invoiceNumber, supplementTime, invoiceMoney, workStateName, userId);
		
		System.out.println(total);
		return total;
	}
	
}
